package com.huluobo.a2207douyindemo.adapter;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.huluobo.a2207douyindemo.R;
import com.huluobo.a2207douyindemo.live.MessageEntity;

/**
 * Created by LC on 2024/9/25.
 */
public class ChatRoomAdapter extends BaseQuickAdapter<MessageEntity, BaseViewHolder> {
    public ChatRoomAdapter() {
        super(R.layout.item_chat_room);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, MessageEntity messageEntity) {
        String name = messageEntity.getName();
        String content = messageEntity.getContent();
        String msg = name + ":" + content;
//        baseViewHolder.setText(R.id.item_chat_room_tv, name + ":" + content);

//        <span color = "red" > 张三 </span ><span color = "blue" fontStyle="bold"> 发送了一条消息 </span >
        //富文本编辑器
        SpannableStringBuilder builder = new SpannableStringBuilder(msg);

        //定义2个颜色
        ForegroundColorSpan blue = new ForegroundColorSpan(Color.BLUE);
        ForegroundColorSpan yellow = new ForegroundColorSpan(Color.YELLOW);
        ForegroundColorSpan red = new ForegroundColorSpan(Color.RED);

        if (name.equals("系统")) {
            //name标记为红色
            builder.setSpan(red, 0, messageEntity.getName().length() + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        } else {
            //name标记为蓝色
            builder.setSpan(blue, 0, messageEntity.getName().length() + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        }
        //content 全部标记为黄色
        builder.setSpan(yellow, messageEntity.getName().length() + 1, msg.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        baseViewHolder.setText(R.id.item_chat_room_tv, builder);
    }
}
